#pragma once
#include<iostream>
#include"Protocol.hpp"

//使用说明：
//ServerCal cal;
//cal.Calculator(package);       //直接返回客户端想要的数据，客户端直接来的数据——>客户端想要的数据

enum
{
    Div_Zero=1,
    Mod_Zero,
    Other_Oper
};

class ServerCal
{
    public:
        Response CalculatorHelper(Request& req)
        {
            int x=req._data1;
            int y=req._data2;
            char op=req._op;
            Response rsq;
            switch (op)
            {
            case '+':
                rsq._result = x + y;
                break;
            case '-':
                rsq._result = x - y;
                break;
            case '*':
                rsq._result = x * y;
                break;
            case '/':
            {
                if (y == 0)
                    rsq._code = Div_Zero;
                else
                    rsq._result = x / y;
                break;
            }
            case '%':
            {
                if (y == 0)
                    rsq._code = Mod_Zero;
                else
                    rsq._result = x % y;
                break;
            }
            default:
                rsq._code = Other_Oper;
            }
            return rsq;
        }

        std::string Calculator(std::string& package)
        {
            if(package.empty())
            {
                return "";
            }
            std::string content;
            DeCode(package,&content);
            Request req;
            req.DeSerialize(content);

            std::cout<<"request: "<<req.GetRequest()<<std::endl;
            Response rsq=CalculatorHelper(req);
            std::cout<<"result: "<<rsq.GetResult()<<std::endl;

            std::string str;
            rsq.Serialize(&str);
            std::string message;
            EnCode(str,&message);

            return message;
        }
};

